<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\partner\controller;

use cmf\controller\AdminBaseController;
use app\partner\model\AreaModel;
use app\partner\model\PartnerModel;
use think\db\Query;
use think\Db;
use app\partner\validate\PartnerValidate;

/**
 * Class AdminIndexController
 * @package app\partner\controller
 * @adminMenuRoot(
 *     'name'   =>'人员管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'group',
 *     'remark' =>'人员管理'
 * )
 */
class AdminIndexController extends AdminBaseController
{
    /**
     * 人员管理
     * @adminMenu(
     *     'name'   => '人员管理',
     *     'parent' => 'partner/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '人员管理',
     *     'param'  => ''
     * )
     * @param userType 人员在系统中的角色  8
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $keywords = $this->request->param('keywords');

        $partnerModel = new PartnerModel();

        $partner = Db::name('partner')->where(function (Query $query) use ($keywords) {
            if ($keywords) {
                $query->where('name', 'like', "%$keywords%");
            }
        })
        ->order("id DESC")
        ->paginate(10);
        $partner->appends(['name' => $keywords]);;


        // 获取分页显示
        $page = $partner->render();


        $this->assign('partner',$partner);
        $this->assign('page',$page);

        return $this->fetch();
        
    }
    /**
     * 人员管理
     * @adminMenu(
     *     'name'   => '添加人员',
     *     'parent' => 'partner/AdminIndex/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加人员',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $area = new AreaModel();
        $province = $area->province();
        $this->assign('province',$province);

        return $this->fetch();
    }

    /**
     * 人员添加提交
     * @adminMenu(
     *     'name'   => '人员添加提交',
     *     'parent' => 'add',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '人员添加提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param("post");

            // $validate = new PartnerValidate();
            // $result = $validate->check($param);
            // if(!$result){
            //     return $this->error($result);
            // }

            $area_code = '';
            $area_value = '';
            if($param['province']!=0){
                $area_code.=$param['province'];
                $area_value.=$param['province_name'];
            }
            if($param['city']!=0){
                $area_code.=','.$param['city'];
                $area_value.=','.$param['city_name'];
            }
            if($param['country']!=0){
                $area_code.=','.$param['country'];
                $area_value.=','.$param['country_name'];
            }

            $data = [
                'name'=>$param['name'],
                'account'=>$param['account'],
                'password'=>cmf_password($param['password']),
                'area_code'=>$area_code,
                'area_value'=>$area_value,
                'create_time'=>time(),
                'status'=>1,
                'tel'=>$param['tel'],
                'idcard'=>$param['idcard'],
                'company'=>$param['company'],
                'money'=>$param['money']*100,
                'unicode'=>$param['unicode']
            ];

            $res = Db::name('partner')->insertGetId($data);
            if($res){
                return $this->success("添加成功");
            }
            else{
                return $this->error("添加失败");

            }
        }
    }

    /**
     * 人员管理
     * @adminMenu(
     *     'name'   => '编辑人员',
     *     'parent' => 'partner/AdminIndex/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '编辑人员',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->param('id',0,'intval');
        if($id>0){
            $partnerModel = new PartnerModel();
            $partner = $partnerModel->getOne($id);
            $this->assign('partner',$partner);
        }
        $area = new AreaModel();
        $province = $area->province();
        $this->assign('province',$province);
        $this->assign('id',$id);

        return $this->fetch();
    }

    /**
     * 人员编辑提交
     * @adminMenu(
     *     'name'   => '人员编辑提交',
     *     'parent' => 'add',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '人员编辑提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param("post");

            // $validate = new PartnerValidate();
            // $result = $validate->check($param);

            // if(!$result){
            //     return $this->error($result);
            // }

            $area_code = '';
            $area_value = '';
            if($param['province']!=0){
                $area_code.=$param['province'];
                $area_value.=$param['province_name'];
            }
            if($param['city']!=0){
                $area_code.=','.$param['city'];
                $area_value.=','.$param['city_name'];
            }
            if($param['country']!=0){
                $area_code.=','.$param['country'];
                $area_value.=','.$param['country_name'];
            }
            
            $data = [
                'tel'=>$param['tel'],
                'idcard'=>$param['idcard'],
                'company'=>$param['company'],
                'money'=>$param['money']*100,
                'unicode'=>$param['unicode']
            ];
            if($area_code!==''){
                $data['area_code']=$area_code;
            }
            if($area_value!==''){
                $data['area_value']=$area_value;
            }
            if($param['password']!=''){
                $data['password']=cmf_password($param['password']);
            }
            
            $res = Db::name('partner')->where('id',$param['id'])->update($data);
            if($res){
                return $this->success("修改成功");
            }
            else{
                return $this->error("修改失败");

            }
        }
    }

        /**
     * 人员缴存管理
     * @adminMenu(
     *     'name'   => '人员缴存管理',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '人员缴存管理',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function editMore()
    {
        $id = $this->request->param('id',0,'intval');

        if($id>0){
            $partner = Db::name('partner_gongjijin')->where('partner_id',$id)->find();

            if($this->request->isPost()){

                $param = $this->request->param('post');
                // unset($param['last_month']);
                if($partner){
                    $res = Db::name('partner_gongjijin')->where('partner_id',$id)->update($param);
                }else{
                    $param['partner_id'] = $id;
                    $res = Db::name('partner_gongjijin')->insertGetId($param);
                    
                }
                if($res){
                    return $this->success('更新成功','index');
                }else{

                    return $this->error('更新失败');
                }
                
            }else{
                if($partner){
                    $this->assign('partner',$partner);
                }else{
                    $this->assign('partner',[
                        // 'base_line'=>'',
                        // 'personal_rate'=>'',
                        // 'company_rate'=>'',
                        // 'personal_money' =>'',
                        // 'company_money'=>'',
                        // 'last_month'=>'',
                    ]);
                }
                $this->assign('id',$id);

                return $this->fetch();
            }
            
        }
       
    }

     /**
     * 停用人员
     * @adminMenu(
     *     'name'   => '停用人员',
     *     'parent' => 'partner/AdminIndex/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '停用人员',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = $this->request->param('id', 0, 'intval');

        if (!empty($id)) {

            $result = Db::name('partner')->where(["id" => $id])->setField('status', '0');
            if ($result !== false) {
                $this->success("停用成功！", url("AdminIndex/index"));
            } else {
                $this->error('停用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

         /**
     * 停用人员
     * @adminMenu(
     *     'name'   => '恢复人员',
     *     'parent' => 'partner/AdminIndex/default',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '恢复人员',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = $this->request->param('id', 0, 'intval');

        if (!empty($id)) {

            $result = Db::name('partner')->where(["id" => $id])->setField('status', '1');
            if ($result !== false) {
                $this->success("恢复成功！", url("AdminIndex/index"));
            } else {
                $this->error('恢复失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

            /**
     * 添加客户跟进记录
     * @adminMenu(
     *     'name'   => '添加客户跟进记录',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '添加客户跟进记录',
     *     'param'  => ''
     * )
     */
    public function addRecord()
    {
        $param = $this->request->param();
        $admin_id = cmf_get_current_admin_id();
        $user = Db::name("user")->where("id", $admin_id)->find();

        $data = [
            'partner_id'=>$param['user_id'],
            'title'=>$param['title'],
            'money'=>$param['money']*100,
            'create_time'=>$param['create_time'],
            'type'=>$param['type'],
            'update_time'=>time()
        ];
        $res = Db::name('partner_gongjijin_record')->insertGetId($data);

        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 修改客户跟进记录
     * @adminMenu(
     *     'name'   => '修改客户跟进记录',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '修改客户跟进记录',
     *     'param'  => ''
     * )
     */
    public function editRecord()
    {
        $param = $this->request->param();
        if(!empty($param['title'])){
            $data = [
                'title'=>$param['title'],
                'create_time'=>$param['create_time'],
                'money'=>$param['money']*100,
                'type'=>$param['type'],
                'update_time'=>time()
            ];
            $res = Db::name('partner_gongjijin_record')->where('id',$param['id'])->update($data);
    
            if($res){
                return $this->success('修改成功',url('AdminIndex/recordsList', ['id' =>$param['partner_id']]));
            }else{
                return $this->error('修改失败');
            }
        }else{
            $record = Db::name('partner_gongjijin_record')->where('id',$param['id'])->find();
            $this->assign('record',$record);
            return $this->fetch();
        }
        
    }
    
    /**
     * 客户跟进记录
     * @adminMenu(
     *     'name'   => '客户跟进记录',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '客户跟进记录',
     *     'param'  => ''
     * )
     */
    public function records()
    {
        $param = $this->request->param();

        $records = Db::name('partner_gongjijin_record')
            ->where('partner_id',$param['id'])
            ->order('create_time desc')
            ->paginate(10);
        $records->appends($param);

        $page = $records->render();

        $this->assign("user_id",$param['id']);
        $this->assign("page",$page);
        $this->assign("records",$records);

        return $this->fetch();
    }

    /**
     * 客户记录
     * @adminMenu(
     *     'name'   => '客户记录列表',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '客户记录列表',
     *     'param'  => ''
     * )
     */
    public function recordsList()
    {
        $param = $this->request->param();

        $records = Db::name('partner_gongjijin_record')
            ->where('partner_id',$param['id'])
            ->order('create_time desc')
            ->paginate(10);
        $records->appends($param);

        $page = $records->render();

        $this->assign("user_id",$param['id']);
        $this->assign("page",$page);
        $this->assign("records",$records);

        return $this->fetch();
    }
}